<template>
  <transition name="fade">
    <div ref="modal" v-if="visible"
         :class="[`max-modal`]"
         @mousedown="mouseDown($event)"
         @mouseup="mouseUp($event)"
         :style="defaultTheme">
      <div class="modal-head">
        {{ title }}
        <span v-if="closable"
              class="close-button"
              @click.stop="close">x</span>
      </div>
      <div v-if="$slots.default" class="modal-body">
        <slot></slot>
      </div>
    </div>
  </transition>
</template>

<script>
import {defineComponent, ref, reactive} from 'vue';

export default defineComponent({
  name: "max-modal",
  setup(props, context) {
    /**
     * 变量列表：
     * defaultTheme：默认主题配置
     * modal：弹窗实例
     * moveData：弹窗坐标数据
     */
    const defaultTheme = reactive({
      '--firstColor': '#1890ff',
      '--secondColor': '#66B1FF',
      '--thirdColor': '#f0f0f0',
      '--fontColor': '#fff',
      '--backGroundColor': '#fff'
    });
    const modal = ref();
    const moveData = reactive({
      x: 0,
      y: 0
    });
    /**
     * 方法列表：
     * init：初始化
     * mouseDown：鼠标按下事件
     * mouseMove：鼠标移动事件
     * mouseUp：鼠标回弹事件
     * close：关闭选中页面
     */
    const init = () => {

    }

    const mouseDown = (event) => {
      moveData.x = event.pageX - modal.value.offsetLeft;
      moveData.y = event.pageY - modal.value.offsetTop;
      window.onmousemove = mouseMove;
    }
    const mouseMove = (event) => {
      let moveLeft = event.pageX - moveData.x + 'px'
      let moveTop = event.pageY - moveData.y + 'px'
      modal.value.style.left = moveLeft
      modal.value.style.top = moveTop
    }
    const mouseUp = (event) => {
      window.onmousemove = null
    }

    const close = () => {
      context.emit("update:visible", false);
    }

    init();
    return {
      defaultTheme,
      modal,
      mouseDown,
      mouseUp,
      close
    }
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: ''
    },
    closable: {
      type: Boolean,
      default: true
    },
    theme: {
      type: Object,
      default: {}
    }
  },
});
</script>
<style scoped>
.max-modal {
  position: fixed;
  min-width: 300px;
  box-shadow: 0 4px 12px var(--thirdColor);
  border: 1px solid var(--thirdColor);
  background-color: var(--backGroundColor);
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 1000;
}

.modal-head {
  margin: 0;
  cursor: pointer;
  word-wrap: break-word;
  text-align: center;
  padding: 12px 24px;
  background: var(--firstColor);
  border-bottom: 1px solid var(--thirdColor);
  color: var(--fontColor);
  font-weight: 500;
  font-size: 16px;
}

.modal-head::before{
  position: absolute;
  content: "";
  left: 0;
  top: 0;
  height: 2px;
  width: 0%;
  background: var(--secondColor);
  transition: all 0.3s ease;
}

.modal-head:hover::before {
  width: 100%;
}

.close-button {
  width: 48px;
  height: 48px;
  position: absolute;
  font-weight: 300;
  font-size: 20px;
  line-height: 44px;
  color: var(--fontColor);
  top: 0px;
  right: 0px;
}

.close-button:hover {
  color: #000;
}

.modal-body{
  padding: 12px;
}

.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.8s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
}
</style>
